#include <stdio.h>
#include <string.h>
#include "algorithm.h"
#include "map.h"

#include "map.c++"
#include "hoistbp.c++"


char* key[10] =
{
  "Zero",
  "One",
  "Two",
  "Three",
  "Four",
  "Five",
  "Six",
  "Seven",
  "Eight",
  "Nine"
};

typedef bool (*cmpfn)(char const*, char const*);

static bool str_less(char const* x, char const* y)
{
  return strcmp(x, y) < 0;
}

void print_elem(map_value_type<char*, int> const& elem)
{
  printf("%s: %i\n", elem.first, elem.second);
}

int main()
{
  map<char*, int, cmpfn> test_map(str_less);

  int i;

  for(i = 0; i < 10; ++i)
    test_map[key[i]] = i;

  for(i = 0; i < 10; ++i)
    printf("%s: %i\n", key[i], test_map[key[i]]);

  printf("\n");

  print_elem(*test_map.find("Three"));

  map<char*, int, cmpfn> const& const_map = test_map;
  print_elem(*const_map.find("Three"));

  try
  {
    map<char*, int, cmpfn> const& const_map = test_map;
    int not_there = const_map["hello world"];
  }
  BEGIN_HANDLERS
  catch(out_of_range, ex)
  {
    printf("Exception caught: %s\n\n", ex.what());
  }
  END_HANDLERS

  for_each(test_map.begin(), test_map.end(), print_elem);
  printf("\n");

  for_each(test_map.rbegin(), test_map.rend(), print_elem);
  printf("\n");

  map_iterator<char*, int, cmpfn> j = test_map.end();

  print_elem(*--j);
  printf("\n");

  print_elem(*--j);
  printf("\n");

  print_elem(*--j);
  printf("\n");

  map<char*, int, cmpfn> m2(test_map);

  for_each(m2.begin(), m2.end(), print_elem);
  printf("\n");

  printf("test_map == m2 %i\n", test_map == m2);
  printf("\n");

  m2["Zero"] = 1;
  for_each(m2.begin(), m2.end(), print_elem);
  printf("\n");

  printf("test_map == m2 %i\n", test_map == m2);
  printf("\n");

  test_map.erase(test_map.find("Five"), test_map.find("Seven"));
  for_each(test_map.begin(), test_map.end(), print_elem);
  printf("\n");

  test_map.erase(test_map.find("Eight"));
  for_each(test_map.begin(), test_map.end(), print_elem);
  printf("\n");

  test_map.erase(test_map.find("Zero"));
  for_each(test_map.begin(), test_map.end(), print_elem);
  printf("\n");
};

